package mysql

import (
	"gorm.io/gorm"
	"mouth-project/global"
)

//商品表

type Goods struct {
	gorm.Model
	GoodsTitle     string       `gorm:"type:varchar(30);comment:商品标题"`
	GoodsPrice     float64      `gorm:"type:decimal(10,2);comment:商品价格"`
	GoodsStock     int64        `gorm:"type:int(10);comment:商品库存"`
	GoodsCover     string       `gorm:"type:varchar(255);comment:商品封面"`
	GoodsDetailsId int64        `gorm:"type:int(10);comment:商品详情Id"`
	GoodsDetails   GoodsDetails `gorm:"foreignKey:GoodsDetailsId"`
}

//商品详情表

type GoodsDetails struct {
	gorm.Model
	GoodsTitle string    `gorm:"type:varchar(30);comment:商品标题"`
	GoodsPrice float64   `gorm:"type:decimal(10,2);comment:商品价格"`
	GoodsStock int64     `gorm:"type:int(10);comment:商品库存"`
	GoodsCover string    `gorm:"type:varchar(255);comment:商品封面"`
	Comment    []Comment `gorm:"foreignKey:GoodsDetailsId"`
}

//评论表

type Comment struct {
	gorm.Model
	GoodsDetailsId int64  `gorm:"type:int(10);comment:商品详情Id"`
	UserId         int64  `gorm:"type:int(10);comment:用户Id"`
	Text           string `gorm:"type:varchar(50);comment:评论内容"`
	Pid            int64  `gorm:"type:int(10);comment:父级Id"`
}

//商品发布

func (g *Goods) Create() error {
	return global.Db.Create(&g).Error
}

// 商品详情

func (g *Goods) Info() error {
	return global.Db.Where("id = ?", g.ID).Find(&g).Limit(1).Error
}

//评论

func (c *Comment) Create() error {
	return global.Db.Create(&c).Error
}
